package com.jilujia.domain.hotel.repository;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.hotel.Hotel;
import com.jilujia.framework.domain.JpaBusinessRepository;

@Repository
@Transactional(readOnly = true)
public interface HotelRepository extends JpaBusinessRepository<Long, Hotel>, HotelRepositoryPlus {

	@Query("select a from Hotel a, UserFollowHotel b, User c where a.id = b.hotelId and b.userId = c.id and c.id=:userId order by a.id ")
	public List<Hotel> findHotelsByUserId(@Param("userId") Long userId);

	@Query("select a from Hotel a order by a.followCount")
	public List<Hotel> findTop10Hotels();

	@Transactional
	@Modifying
	@Query("update Hotel a set a.followCount=a.followCount+1 where a.id = :hotelId")
	public int addFollowCount(@Param("hotelId") Long hotelId);

	@Transactional
	@Modifying
	@Query("update Hotel a set a.followCount=a.followCount-1 where a.id = :hotelId")
	public int subFollowCount(@Param("hotelId") Long hotelId);

	@Query("select a from Hotel a where a.region.id = :regionId order by a.followCount")
	public List<Hotel> findHotelsByRegion(@Param("regionId") Long regionId, Pageable pageable);
}
